package com.android.graffiti;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.common.base.utils.DeviceUtils;
import com.photoeditor.R;
import com.photoeditor.demo.model.image.util.ImageHelper;


/**
 * 在涂鸦界面中用作颜色及画笔选择的弹出框控件
 */
public class SelectorPopupView extends LinearLayout {

    private int mType;

    private RadioGroup mRadioGroup;
    
    public SelectorPopupView(Context context) {
        super(context);
    }
    
    public SelectorPopupView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    
    /**
     * 初始化
     */
    public void initialize(int layoutId) {
        // TODO Auto-generated method stub
        LayoutInflater li = (LayoutInflater)getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        li.inflate(layoutId, this, true);
        
        mRadioGroup = (RadioGroup) findViewById(R.id.graffito_popup_radio_group);
        countFitMargin(mRadioGroup, 8);
    }


    private void countFitMargin(RadioGroup group, int margin){
        int count = group.getChildCount();
        int defaultSize = 32;
        if(count * DeviceUtils.dip2px(getContext(), defaultSize + margin * 2) <= ImageHelper.SCREEN_WIDTH){

        } else {
            float number = ImageHelper.SCREEN_WIDTH / DeviceUtils.dip2px(getContext(), defaultSize + margin * 2f);
            float mini = number - (int) number;
            if (mini >= 0.5f && mini <= 0.65f) {
            } else {
                int width;
                width = (int) (ImageHelper.SCREEN_WIDTH / ((int) number + 0.58f));

                int newMargin = (width - DeviceUtils.dip2px(getContext(), defaultSize)) / 2;
                for (int i = 0; i < count; i++) {
                    View v = group.getChildAt(i);
                    RadioGroup.LayoutParams lp = (RadioGroup.LayoutParams) v.getLayoutParams();
                    lp.leftMargin = newMargin;
                    lp.rightMargin = newMargin;
                    v.setLayoutParams(lp);
                }
            }
        }
    }
    
    /**
     * 获取该控件对应的操作类型
     * @return 该控件对应的操作类型
     */
    public int getType() {
        return mType;
    }
    
    /**
     * 设置该控件对应的操作类型
     * @param type 该控件对应的操作类型
     */
    public void setType(int type) {
        this.mType = type;
    }
    
    
    /**
     * 添加一个{@link RadioButton}到视图中
     * @param button 要添加的按钮
     */
    public void addRadioButton(RadioButton button) {
        if(mRadioGroup != null) {
            mRadioGroup.addView(button);
        }
    }
    
    /**
     * 清空所有的{@link RadioButton}
     */
    public void clearRadioButtons() {
        if(mRadioGroup != null) {
            mRadioGroup.removeAllViews();
        }
    }
    
    /**
     * 获取指定序号的{@link RadioButton}
     * @param index 指定的序号
     */
    public RadioButton getRadioButton(int index) {
        if(mRadioGroup != null) {
            return (RadioButton) mRadioGroup.getChildAt(index);
        } else {
            return null;
        }
    }
    
    /**
     * 获取指定{@link RadioButton}的序号
     * @param button 指定的{@link RadioButton}
     * @return 指定的{@link RadioButton}的序号 
     */
    public int indexOfRadioButton(RadioButton button) {
        if(mRadioGroup != null && button != null) {
            return mRadioGroup.indexOfChild(button);
        } else {
            return -1;
        }
    }
    
    /**
     * 设置单选项改变的监听器
     * @param l 单选项改变的监听器
     */
    public void setOnRadioCheckedListener(RadioGroup.OnCheckedChangeListener l) {
        if(mRadioGroup != null) {
            mRadioGroup.setOnCheckedChangeListener(l);
        }
    }
    
    
    /**
     * 设置要选中的按钮
     * @param index 要选中的按钮的序号
     */
    public void setButtonChecked(int index, boolean checked) {
        RadioButton button = getRadioButton(index);
        if(button != null) {
            button.setChecked(checked);
        }
    }
    
    
    /**
     * 获取选中的按钮的序号
     * @return 选中的按钮的序号
     */
    public int getCheckedIndex() {
        if(mRadioGroup != null) {
            int btnId = mRadioGroup.getCheckedRadioButtonId();
            RadioButton button = (RadioButton) mRadioGroup.findViewById(btnId);
            return indexOfRadioButton(button);
        } else {
            return -1;
        }
    }
    
    
    /**
     * 显示该控件
     */
    public void show() {
        this.setVisibility(View.VISIBLE);
    }
    
    
    /**
     * 隐藏该控件
     */
    public void hide() {
        this.setVisibility(View.INVISIBLE);
    }
    
    
    /**
     * 尝试隐藏该控件，如果被固定将不会隐藏
     */
    public void tryHide() {
        if(this.getVisibility() == View.VISIBLE) {
            this.setVisibility(View.INVISIBLE);
        }
    }
}
